package edu.pku.sei.transformation.editor.controller.commands;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.transformation.editor.model.Expression;
import edu.pku.sei.transformation.editor.model.base.NamedElement;

public class ChangeNameCommand extends Command {
	private Object obj;
	private String name;
	private String oldName;

	public ChangeNameCommand(String name, Object obj) {
		super("change name");
		this.obj = obj;
		this.name = name;
	}

	@Override
	public boolean canExecute() {
		return true;
	}

	@Override
	public void execute() {
		if (obj instanceof NamedElement) {
			oldName = ((NamedElement) obj).getName();
			((NamedElement) obj).setName(name);
		} else if (obj instanceof Expression) {
			oldName = ((Expression) obj).getExp();
			((Expression) obj).setExp(name);
		}
	}

	@Override
	public void undo() {
		if (obj instanceof NamedElement) {
			((NamedElement) obj).setName(oldName);
		} else if (obj instanceof Expression) {
			((Expression) obj).setExp(oldName);
		}
	}
}
